package com.hxexp.weixin.mvc.pay.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.hxexp.weixin.common.cache.PassengerTicketCacheManager;
import com.hxexp.weixin.common.config.DeployConfig;
import com.hxexp.weixin.common.exception.APIException;
import com.hxexp.weixin.common.exception.ServiceException;
import com.hxexp.weixin.common.math.RandomUtil;
import com.hxexp.weixin.hpe.api.orderpayment.OrderPaymentAPI;
import com.hxexp.weixin.hpe.api.orderpayment.model.PrepayOrderParamBean;
import com.hxexp.weixin.hpe.api.orderpayment.model.PrepayOrderResultBean;
import com.hxexp.weixin.mvc.pay.service.PayService;
import com.qq.weixin.common.security.WeixinSignatureUtil;
import com.qq.weixin.constant.AppDevConstant;

/**
 * 支付服务实现类
 * @author li.jiang
 *
 */
@Service
public final class PayServiceImpl implements PayService {
    private static final String MAPKEY_APPID = "appId";
    private static final String MAPKEY_PAYSIGN = "paySign";

    @Autowired
    private OrderPaymentAPI orderPaymentAPI;
    @Autowired
    PassengerTicketCacheManager pticketCacheManager;
    @Autowired
    private DeployConfig deployConfig;
    /* (non-Javadoc)
     * @see com.hxexp.weixin.mvc.pay.service.PayService#prepayOrder(com.hxexp.weixin.model.hpe.orderpayment.PrepayOrderParamBean)
     */
    @Override
    public PrepayOrderResultBean prepayOrder(PrepayOrderParamBean prepayOrderParamBean) throws ServiceException {
        PrepayOrderResultBean prepayOrderResult = new PrepayOrderResultBean();

        try {
            prepayOrderResult = orderPaymentAPI.prepayOrder(prepayOrderParamBean);
        } catch (APIException e) {
            throw new ServiceException(e);
        }

        return prepayOrderResult;
    }

    @Override
    public void savePassengerTicketRecord(String ptiks) throws ServiceException {

        if (!StringUtils.isEmpty(ptiks)) {
            Map<String, String> passengerTicketInfoMap = new HashMap<String, String>();
            try {
                String[] purchasedTicketInfoArray = ptiks.split(",");
                for (String purchasedTicketInfo : purchasedTicketInfoArray) {
                    String[] passengerTicketInfoKV = purchasedTicketInfo.split("=");
                    passengerTicketInfoMap.put(passengerTicketInfoKV[0], passengerTicketInfoKV[1]);
                }
            } catch (Exception e) {
                throw new ServiceException(e);
            }
            pticketCacheManager.batchPut(passengerTicketInfoMap);
        }

    }

    /*
     * (non-Javadoc)
     *
     * @see com.hxexp.weixin.mvc.pay.service.PayService#generatePayParam(java.lang.String)
     */
    @Override
    public Map<String, String> generatePayParam(String prepayId) {
        final Map<String, String> payParamMap = new HashMap<String, String>();

        payParamMap.put(MAPKEY_APPID, AppDevConstant.getAppId(deployConfig.getDeployEnvironment()));
        payParamMap.put("timeStamp", System.currentTimeMillis() / 1000 + "");
        payParamMap.put("nonceStr", RandomUtil.getRandomString(32));
        payParamMap.put("package", "prepay_id=" + prepayId);
        payParamMap.put("signType", "MD5");
        payParamMap.put(MAPKEY_PAYSIGN,
                WeixinSignatureUtil.generatePaySign(AppDevConstant.getPaySecretKey(deployConfig.getDeployEnvironment()), payParamMap));

        return payParamMap;
    }

}
